<?php

namespace app\common\command;

use app\common\classes\BehaviorRedis;
use app\common\classes\VoucherRedis;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;

class NovelStat extends Command
{

    protected function configure()
    {
        $this->setName('novel_stat')
            ->addArgument('action', Argument::OPTIONAL, "方法名称")
            ->addOption('start_date', null, Option::VALUE_REQUIRED, '开始日期')
            ->addOption('end_date', null, Option::VALUE_REQUIRED, '结束日期')
            ->setDescription('小说数据统计');
    }

    protected function execute(Input $input, Output $output)
    {

        $action = $input->getArgument('action');

        if (method_exists($this, $action)) {
            $start_date = $input->hasOption('start_date') ? $input->getOption('start_date') : date('Y-m-d', strtotime('-1days'));
            $end_date = $input->hasOption('end_date') ? $input->getOption('end_date') : date('Y-m-d', strtotime('-1days'));
            $this->$action($start_date, $end_date);
        }

    }

    /**
     * 书籍每日数据
     * @param $start_date
     * @param $end_date
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function daily_novel($start_date, $end_date)
    {
        $date_rang = getDateByRange($start_date, $end_date);
        foreach ($date_rang as $date) {
            $nids = Db::table("novel")->column('id');
            foreach ($nids as $nid) {
                $save = [];
                $save['uv'] = $this->getBookUv($nid, $date);
                $save['money'] = $this->getMoney($nid, $date);
                $save['cuv'] = $this->getCuv($nid, $date);
                $save['pay_uv'] = $this->getPayUv($nid, $date);
                $save['use_coin'] = $this->getUseCoin($nid, $date);
                $save['new_uv'] = $this->getNewUv($nid, $date);
                $save['new_pay_uv'] = $this->getNewPayUv($nid, $date);
                $save['new_money'] = $this->getNewMoney($nid, $date);
                $dWhere = [];
                $dWhere[] = ['nid', '=', $nid];
                $dWhere[] = ['read_date', '=', $date];
                $flag = Db::table("rp_novel_date_daily")->where($dWhere)->value('id');
                if ($flag) {
                    Db::table("rp_novel_date_daily")->where($dWhere)->update($save);
                } else {
                    if ($save['uv'] || $save['money'] || $save['cuv'] || $save['pay_uv'] || $save['use_coin'] || $save['new_uv'] || $save['new_pay_uv'] || $save['new_money']) {
                        $save['nid'] = $nid;
                        $save['read_date'] = $date;
                        Db::table("rp_novel_date_daily")->where($dWhere)->insert($save);
                    }
                }
                echo "日期：{$date}，书籍：{$nid} 处理完成" . PHP_EOL;
            }
        }
        //
    }


    /**
     * 获取书籍uv
     * @param $nid
     * @param $date
     * @return int
     */
    public function getBookUv($nid, $date)
    {
        $today = date("Y-m-d");
        if ($today > "2024-04-08"){
            $redis = new BehaviorRedis();//新使用行为redis
        }else{
            $redis = new VoucherRedis();//消费凭证不让用了，会定时截断
        }
        if ($date <= "2023-12-11" || in_array($date, ['2024-03-25', '2024-03-26'])) {
            $month = date("Ym", strtotime($date));
            $where[] = ['novel_id', '=', $nid];
            $where[] = ['read_date', '=', $date];
            $uidArr = Db::table("quick_user_read_record_{$month}")->where($where)->column('uid');
            return count(array_unique($uidArr));
        } else {
            return $redis->getAllBookUvCount($nid, date("Ymd", strtotime($date)));
        }
    }

    /**
     * 书籍消费uv
     * @param $nid
     * @param $date
     * @return int
     */
    public function getCuv($nid, $date)
    {
        $month = date("Ym", strtotime($date));
        $where[] = ['novel_id', '=', $nid];
        $where[] = ['read_date', '=', $date];
        $where[] = ['pay_read', '=', 1];
        $uidArr = Db::table("quick_user_read_record_{$month}")->where($where)->column('uid');
        return count(array_unique($uidArr));
    }

    /**
     * 书籍支付uv
     * @param $nid
     * @param $date
     * @return int
     */
    public function getPayUv($nid, $date)
    {
        $where[] = ['nid', '=', $nid];
        $where[] = ['create_date', '=', $date];
        $uidArr = Db::table("pay_order_success")->where($where)->column('uid');
        return count(array_unique($uidArr));
    }

    /**
     * 当日充值金额
     * @param $nid
     * @param $date
     * @return float|int
     */
    public function getMoney($nid, $date)
    {
        $where[] = ['nid', '=', $nid];
        $where[] = ['create_date', '=', $date];
        return Db::table("pay_order_success")->where($where)->sum('total_fee') ?? 0;
    }

    /**
     * 当日注册用户充值金额
     * @param $nid
     * @param $date
     * @return float|int
     */
    public function getNewMoney($nid, $date)
    {
        $where[] = ['nid', '=', $nid];
        $where[] = ['create_date', '=', $date];
        $where[] = ['user_reg_date', '=', $date];
        return Db::table("pay_order_success")->where($where)->sum('total_fee') ?? 0;
    }


    /**
     * 获取消费书币数量
     * @param $nid
     * @param $date
     * @return float|int
     */
    public function getUseCoin($nid, $date)
    {
        $month = date("Ym", strtotime($date));
        $where[] = ['novel_id', '=', $nid];
        $where[] = ['read_date', '=', $date];
        $where[] = ['pay_read', '=', 1];
        return Db::table("quick_user_read_record_{$month}")->where($where)->sum('used_all') ?? 0;
    }

    /**
     * 新注册
     * @param $nid
     * @param $date
     * @return float|int|string
     */
    public function getNewUv($nid, $date)
    {
        $where[] = ['source_novel_id', '=', $nid];
        $where[] = ['regdate', '=', $date];
        return Db::table("quick_user")->where($where)->count() ?? 0;
    }


    /**
     * 新注册并支付
     * @param $nid
     * @param $date
     * @return float|int|string
     */
    public function getNewPayUv($nid, $date)
    {
        $where[] = ['nid', '=', $nid];
        $where[] = ['create_date', '=', $date];
        $where[] = ['user_reg_date', '=', $date];
        $uidArr = Db::table("pay_order_success")->where($where)->column('uid');
        return count(array_unique($uidArr));
    }
}